package org.robot.scm.pojos;

import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.Optional;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.robot.scm.pojos.enums.ChannelEnum;
import org.robot.scm.pojos.enums.HttpMethodEnum;

/**
 * 目标通知类，面向通知
 */
@Builder
@Data
@AllArgsConstructor
@NoArgsConstructor
public class TargetNotify {

    private HttpMethodEnum httpMethodEnum;

    /**
     * 通知渠道
     */
    private ChannelEnum channelEnum;

    /**
     * 通知地址
     */
    private String notifyUrl;

    /**
     * 通知设备SN
     */
    private String deviceSn;

    /**
     * 通知主题
     */
    private String topic;

    /**
     * 通知内容
     */
    private String content;

    /**
     * 通知明细地址
     */
    private String detailUrl;

    /**
     * 通知图标
     */
    private String icon;

    public String toUrl() {
        try {
            String encodeDetail = Optional.ofNullable(detailUrl).map(e -> URLEncoder.encode(e, StandardCharsets.UTF_8)).orElse("");
            String encodeContent = URLEncoder.encode(content, StandardCharsets.UTF_8);
            String encodeIcon = Optional.ofNullable(icon).map(e -> URLEncoder.encode(e, StandardCharsets.UTF_8)).orElse("");
            return String.format(notifyUrl, deviceSn, topic, encodeContent, encodeDetail, encodeIcon);
        } catch (Exception e) {
            return notifyUrl;
        }
    }

}
